//
//  StatusCell.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/14.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    var bottomConstrains : Constraint?
    
    //数据模型
    var statues : Status? {
        
        didSet{
            
            topView.statues = statues
            self.bottomConstrains?.uninstall()
            
            if let re = statues?.retweeted_status {
                
                reweentedView.reweentedStatus = re
                
                reweentedView.hidden = false
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrains = make.top.equalTo(reweentedView.snp_bottom).constraint
                })
  
            }else{
                
                reweentedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrains = make.top.equalTo(topView.snp_bottom).constraint
                })

            }
            
            
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .None
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   //设置子控件
    func setupUI(){
        
       contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(reweentedView)
        
        
        topView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
            
        }
       
        reweentedView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(topView.snp_bottom)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
            self.bottomConstrains  =  make.top.equalTo(reweentedView.snp_bottom).constraint
        }
        
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(bottomView.snp_bottom)
            
            
        }
        
        
    }
    
    //懒加载控件
    
    lazy var topView :StatusOriginalView = StatusOriginalView()
    
    lazy var bottomView :StatusBottomView = StatusBottomView()
    
    lazy var reweentedView :ReweetedView = ReweetedView()
}
